// Fill out your copyright notice in the Description page of Project Settings.


#include "UserWidget/WidgetComponent/OnMissionFinishedWidget.h"

#include "Animation/WidgetAnimation.h"
#include "Components/Border.h"
#include "Components/TextBlock.h"
#include "Libraries/CommonLibrary.h"

void UOnMissionFinishedWidget::NativeOnInitialized() {
	Super::NativeOnInitialized();
	DisplayTime = 4.f;
}

void UOnMissionFinishedWidget::DisplayOnMissionFinishedWidget(FMissionMaster LCurrentMission, EMissionEnd LResult)
{
	if (GetWorld()->GetTimerManager().IsTimerActive(FirstHandle) || GetWorld()->GetTimerManager().IsTimerActive(SecondHandle)) return;
	CurrentMission = LCurrentMission;
	Result = LResult;
	FText HeadlineText;
	switch (Result) {
	case EMissionEnd::Failed:
		HeadlineText = FText::FromString(FString("Q U E S T  F A I L E D"));
		break;
	case EMissionEnd::Succeed:
		HeadlineText = FText::FromString(FString("Q U E S T  C O M P L E T E D"));
		break;
	default:
		break;
	}
	Headline->SetText(HeadlineText);
	DisplayTime = CurrentMission.OnMissionFinished.WidgetLayout.DisplayTime;
	const UEnum* LRegion = StaticEnum<ERegion>();
	int32 Index = LRegion->GetIndexByValue((int64)CurrentMission.MissionMainInfo.Region);
	Region->SetText(LRegion->GetDisplayNameTextByIndex(Index));
	Secondline->SetText(CurrentMission.MissionMainInfo.Name);
	CenterBorder->SetBrushColor(CurrentMission.OnMissionFinished.WidgetLayout.BorderColor);
	FLinearColor TempColor;
	switch (Result) {
	case EMissionEnd::Failed:
		TempColor = FLinearColor(0.5625f, 0.055664f, 0.055664f,1.f);
		break;
	case EMissionEnd::Succeed:
		TempColor = CurrentMission.OnMissionFinished.WidgetLayout.HeadlineColor;
		break;
	default:
		TempColor = FLinearColor(1.f, 1.f, 1.f, 1.f);
		break;
	}
	Headline->SetFont(MakeOutlineColor(Headline->GetFont(), TempColor));
	Headline->SetColorAndOpacity(FSlateColor(TempColor));

	UWidgetAnimation* DisplayAnim = UCommonLibrary::GetAnimInstance(GetClass(), FText::FromString("DisplayAnim"));
	PlayAnimation(DisplayAnim);
	FTimerDelegate RespawnDelegate = FTimerDelegate::CreateUObject(this, &UOnMissionFinishedWidget::FirstDelayLater);
	GetWorld()->GetTimerManager().SetTimer(FirstHandle, RespawnDelegate, DisplayAnim->GetEndTime() + DisplayTime, false);
}

void UOnMissionFinishedWidget::FirstDelayLater() {
	UWidgetAnimation* DisplayAnim = UCommonLibrary::GetAnimInstance(GetClass(), FText::FromString("DisplayAnim"));
	PlayAnimation(DisplayAnim, 0.f, 1.f, EUMGSequencePlayMode::Reverse);
	FTimerDelegate RespawnDelegate = FTimerDelegate::CreateUObject(this, &UOnMissionFinishedWidget::SecondDelayLater);
	GetWorld()->GetTimerManager().SetTimer(SecondHandle, RespawnDelegate, DisplayAnim->GetEndTime() + 0.25f, false);
	GetWorld()->GetTimerManager().ClearTimer(FirstHandle);
}

void UOnMissionFinishedWidget::SecondDelayLater() {
	RemoveFromParent();
	GetWorld()->GetTimerManager().ClearTimer(SecondHandle);
}

FSlateFontInfo UOnMissionFinishedWidget::MakeOutlineColor(FSlateFontInfo SlateFontInfo, FLinearColor OutlineColor) {
	FSlateFontInfo LResult = SlateFontInfo;
	LResult.OutlineSettings.OutlineColor = FLinearColor(OutlineColor - FLinearColor(0.2f, 0.2f, 0.2f, 0.5f));
	return LResult;
}
